/**
 * @author: gao
 * @create-date: 2025/3/9 22:35
 */
class ListNode{
    int val;
    ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public ListNode removeElements(ListNode head,int val){
        if(head == null){
            return null;
        }

        ListNode dest = head;
        ListNode cur = head.next;

        while(cur != null){
            if(cur.val == val){
                dest.next = cur.next;
                cur = cur.next;
            }else{
                dest = cur;
                cur = cur.next;
            }
        }

        if(head.val == val){
            head = head.next;
        }
        return head;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(6);
        ListNode node4 = new ListNode(3);
        ListNode node5 = new ListNode(4);
        ListNode node6 = new ListNode(5);
        ListNode node7 = new ListNode(6);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;

        Solution solution = new Solution();
        solution.removeElements(node1,6);

        ListNode NewHead = node1;
    }
}
